import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import compressPlugin from 'vite-plugin-compression' // 开启gzip、br压缩
import styleImport from 'vite-plugin-style-import'
import vueI18n from '@intlify/vite-plugin-vue-i18n'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const config = loadEnv(mode, './')
  return {
    plugins: [
      vue(),
      vueI18n({
        // if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
        // compositionOnly: false,

        // you need to set i18n resource including paths !
        include: path.resolve(__dirname, './src/locales/**')
      }),
      styleImport({
        libs: [
          {
            libraryName: 'vant',
            esModule: true,
            resolveStyle: (name) => `../es/${name}/style`
          }
        ]
      }),
      compressPlugin({
        ext: '.gz', //gz br
        algorithm: 'gzip', //brotliCompress gzip
        deleteOriginFile: false
      })
    ],
    resolve: {
      alias: {
        '@': `${path.resolve(__dirname, 'src')}/`
      }
    },
    server: {
      port: 4004,
      proxy: {
        '/basice': {
          target: config.VITE_BASIC_URL_API,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/basice/, '')
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/style/sassVar.scss" as *;`
        }
      }
    },
    base: './',
    build: {
      target: 'es2015',
      outDir: 'sap',
      assetsDir: 'assets',
      sourcemap: config.VITE_BASIC_ENV == 'staging' ? true : false, //构建后是否生成 source map 文件
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      rollupOptions: {
        output: {
          chunkFileNames: 'js/[name].[hash].js',
          entryFileNames: 'js/[name].[hash].js'
        }
      }
    }
  }
})
